package com.example.myapplication.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.adapter.recyclerViewAdapter.ArtistListAdapter;
import com.example.myapplication.api.MusicApiService;
import com.example.myapplication.entity.Artist;
import com.example.myapplication.entity.BaseResponse;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ArtistListActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private TextView appbar;
    private  ArtistListAdapter adapter;
    private Context context;
    private List<Artist> artistList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_artist_list);
        context=this;
        recyclerView = findViewById(R.id.recycler_view_hotsong);
        appbar = findViewById(R.id.tittle);
        appbar.setText("音乐排行");
        RecyclerView.LayoutManager manager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(manager);
        adapter = new ArtistListAdapter(context);
        recyclerView.setAdapter(adapter);
        loadData();
        adapter.setBaseOnclickListener(new BaseOnclickListener() {
            @Override
            public void onclick(int position) {
                Intent intent = new Intent(context, SingerDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("singer",  artistList.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

    }
    private void loadData(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://musicapi.leanapp.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MusicApiService service =  retrofit.create(MusicApiService.class);
        Call<BaseResponse> call = service.getArtistList(30);
        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                BaseResponse baseResponse = response.body();
                artistList = baseResponse.getArtists();
                adapter.setData(artistList);
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {
                Toast.makeText(getApplicationContext(), "请求失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
}